package com.webcash.CNBoard.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.DeptBoard;
import com.webcash.CNBoard.Model.DeptComment;
import com.webcash.CNBoard.Model.EmpBoard;
import com.webcash.CNBoard.Repository.BoardRepository;
import com.webcash.CNBoard.Repository.DepartmentRepository;


@Service
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	DepartmentRepository departmentRepository;
	
	public List<Department> getDepartmentList() {
		
		return departmentRepository.selectDepartmentList();
	}

	public Department getDepartmentByDeptNo(int deptNo) {
		return departmentRepository.selectDepartmentByDeptNo(deptNo);
	}

	public List<DeptBoard> getDepartmentBoardListByDepartmentNo(int deptNo, int pageNum) {
		return departmentRepository.selectDepartmentBoardListByDepartmentNo(deptNo, pageNum);
	}

	public DeptBoard getDepartmentBoardContentsByBoardNo(int deptBoardNo) {
		return departmentRepository.selectDepartmentBoardContentsByBoardNo(deptBoardNo);
	}

	public void addDeptBoardByEmpNo(String id, Board board, int deptNo) {
		int maxDeptBoardNo=departmentRepository.selectDepartmentBoardNoMaxByDeptNo(deptNo);
		System.out.println("maxDeptBoardNo:"+maxDeptBoardNo);   //3
		int deptBoardNo = maxDeptBoardNo+1;
		departmentRepository.insertDepartmentBoardByDeptNo(id, board, deptNo,deptBoardNo); // id, board, 1, 3
		System.out.println("DepartmentServiceImpl deptNo:"+deptNo);   //1
	}

	public int getDepartmentBoardCountByDeptNo(int deptNo) {
		
		return departmentRepository.selectDepartmentBoardCountByDeptNo(deptNo);
	}

	public DeptBoard getDepartmentBoardByDeptNoAndDeptBoardNo(int deptNo,
			int deptBoardNo) {
		return departmentRepository.selectDepartmentBoardByDeptNoAndDeptBoardNo(deptNo, deptBoardNo);
	}

	public void updateDeptBoardByDeptBoardNo(Board board, int deptBoardNo, int deptNo) {
		departmentRepository.updateDeptBoardByDeptBoardNo(board, deptBoardNo, deptNo);
	}

	public void deleteDeptBoardByDeptNoAndDeptBoardNo(int deptNo, int deptBoardNo) {
		DeptBoard deptBoard = departmentRepository.selectDepartmentBoardByDeptNoAndDeptBoardNo(deptNo, deptBoardNo);
		
		departmentRepository.deleteDeptBoardByDeptNoAndDeptBoardNo(deptNo, deptBoardNo, deptBoard.getBoardNo());
	}

	public void updateDeptBoardForReadCountByDeptBoardNo(int deptNo, int deptBoardNo) {
		DeptBoard deptBoard = departmentRepository.selectDepartmentBoardByDeptNoAndDeptBoardNo(deptNo, deptBoardNo);
		int readCount = deptBoard.getReadCount();
		readCount++;
		
		departmentRepository.updateDeptBoardForReadCountByDeptBoardNo(deptNo, deptBoardNo, readCount);
	}

	public void addDeptCommentByBoardNoAndId(int boardNo, String id,
			String commentContents) {
		departmentRepository.insertDeptCommentByBoardNoAndId(boardNo, id, commentContents);
		DeptBoard board = departmentRepository.selectDepartmentBoardContentsByBoardNo(boardNo);
		int commentCount = board.getCommentCount();
		commentCount++; //commentCount 증가
		departmentRepository.updateDeptBoardForCommentCountByBoardNo(boardNo, commentCount);
	}

	public List<DeptComment> getDeptCommentListByBoardNo(int boardNo) {
		return departmentRepository.selectDeptCommentListByBoardNo(boardNo);
	}

	public void insertDepartmentbyDept(Department dept) {
		departmentRepository.insertDepartment(dept);
	}
	
	
	
}
